/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tletlis.controller;


import java.io.FileNotFoundException;
import java.io.IOException;
import tletlis.UI.Highscore;
import tletlis.model.*;
import tletlis.model.Area.BrickEvent;
import tletlis.view.GamePanel;


/**
 * Class keeping track of the levels, rows and points. Has a area object which
 * which is used to update the game.
 * @author Gustav
 */
public class Controller {
    
    
    private Area gameBoard;
    
    private int score;
    private int rows;
    private int totRows;

    //Opponent
    private int opponentScore;
    private int opponentTotRows;

    private int level;

    private int speed;

    private String playerName;

    private boolean Active;

  /**
   * Constructor for the controller class with parameters for controlling the
   * area class suppossed to be used.Initial speed of the game is also set in this
   * constructor.
   * @param width The width of the area suppossed to be used.
   * @param nr The amount of horizontal blocks the gameboard should consist of.
   * @param height The height of the area suppossed to be used.
   */
    public Controller(int width, int nr, int height) {
        score = 0;
        rows = 0;
        totRows = 0;
        level = 1;
        gameBoard = new Area(width,nr,height);
        new Keys(gameBoard.getComps(),this);
        speed = 500;
        opponentScore=0;
        opponentTotRows=0;
    }

  /**
   * Method for updating the game. The nr is the amount of rows the oponent
   * has constructed. The parameter is 0 for single player games.
   * @param nr The amount of rows that the oponent has constructed.
   * @return int
   */
    public int updateGame (int nr) throws FileNotFoundException, IOException {
        if (nr == -1) {
            gameBoard.win();
            return 0;
        }
        rows += gameBoard.update(BrickEvent.DOWN, nr);
        if(rows == -1) {
            new Highscore(playerName,score);
            return -1;
        }
        if(rows==1)
            score += 10*level;
        else if(rows > 1)
            score += Math.pow((10*level),rows)/4;
        
        totRows += rows;
        if(totRows%10 == 0 && totRows != 0 && rows > 0) {
            level++;
            if(speed > 25)
                speed -= 25;
        }
        //Calculate opponents score and rows
        opponentTotRows +=nr;
        if(nr==1)
            opponentScore += 10*level;
        else if(nr>1)
            opponentScore+=Math.pow((10*level),nr)/4;
        //Set values
        gameBoard.updateScore(score, totRows);
        gameBoard.updateOponentScore(opponentScore, opponentTotRows);
        int temp = rows;
        rows = 0;
        return temp;
    }
    

  /**
   * Method that updates the game due to a keyevent.
   * @param event The keyevent that has taken place.
   */
    protected void GameUpdate(BrickEvent event) {
        rows += gameBoard.update(event,0);
    }

  /**
   * Method that returns true if the game is active
   * @return boolean
   */
    public boolean isAlive() {
        return Active;
    }

  /**
   * Method that returns the current speed of the game.
   * @return int
   */
    int getSpeed() {
        return speed;
    }

  /**
   * Method that sets the players name on the GUI.
   * @param name The players name.
   */
    public void setName(String name) {
        playerName = name;
        gameBoard.setName(name);
    }

  /**
   * Method that sets the oponent players name.
   * @param name The oponent players name.
   */
    public void setOponentName(String name){
        gameBoard.setOponentName(name);
    }

    /**
   * Method that sets the local computers ip.
   * @param ip The ip of the local computer.
   */
    public void setIpLabel(String ip){
        gameBoard.setIpLabel(ip);
    }
    
}
